


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: AbstractFileDataStore</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.datastore.file</a> ]
</div>

<h1>Coverage Summary for Class: AbstractFileDataStore (at.schauer.gregor.vienna.datastore.file)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">AbstractFileDataStore</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    91.7%
  </span>
  <span class="absValue">
    (11/ 12)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    86.5%
  </span>
  <span class="absValue">
    (32/ 37)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.datastore.file;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContext;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.datastore.StorageMetadata;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.exception.ViennaException;
<i>21</i>&nbsp;import org.apache.commons.io.FileUtils;
<i>22</i>&nbsp;import org.apache.commons.io.FilenameUtils;
<i>23</i>&nbsp;import org.apache.commons.io.IOCase;
<i>24</i>&nbsp;import org.apache.commons.io.filefilter.CanReadFileFilter;
<i>25</i>&nbsp;import org.apache.commons.io.filefilter.FileFileFilter;
<i>26</i>&nbsp;import org.apache.commons.io.filefilter.FileFilterUtils;
<i>27</i>&nbsp;
<i>28</i>&nbsp;import java.io.File;
<i>29</i>&nbsp;import java.io.FileFilter;
<i>30</i>&nbsp;import java.io.IOException;
<i>31</i>&nbsp;import java.util.ArrayList;
<i>32</i>&nbsp;import java.util.Collection;
<i>33</i>&nbsp;import java.util.List;
<i>34</i>&nbsp;
<i>35</i>&nbsp;/**
<i>36</i>&nbsp; * @author Gregor Schauer
<i>37</i>&nbsp; */
<i>38</i>&nbsp;public abstract class AbstractFileDataStore&lt;T&gt; implements FileDataStore&lt;T&gt; {
<i>39</i>&nbsp;	protected ViennaContext ctx;
<i>40</i>&nbsp;	protected File rootDirectory;
<i>41</i>&nbsp;	protected String ext;
<i>42</i>&nbsp;	protected FileFilter fileFilter;
<i>43</i>&nbsp;
<b class="fc"><i>44</i>&nbsp;	public AbstractFileDataStore(ViennaContext ctx, File rootDirectory, String ext) {</b>
<b class="fc"><i>45</i>&nbsp;		this.ctx = ctx;</b>
<b class="fc"><i>46</i>&nbsp;		this.rootDirectory = rootDirectory;</b>
<b class="fc"><i>47</i>&nbsp;		this.setExt(ext);</b>
<b class="fc"><i>48</i>&nbsp;	}</b>
<i>49</i>&nbsp;
<i>50</i>&nbsp;	@Override
<i>51</i>&nbsp;	public &lt;E extends T&gt; E load(Class&lt;E&gt; type, Object identifier) {
<b class="fc"><i>52</i>&nbsp;		StorageMetadata&lt;E&gt; metadata = StorageMetadata.create(ctx, type);</b>
<b class="fc"><i>53</i>&nbsp;		metadata.identifier = identifier;</b>
<b class="fc"><i>54</i>&nbsp;		return read(resolveFile(metadata), metadata);</b>
<i>55</i>&nbsp;	}
<i>56</i>&nbsp;
<i>57</i>&nbsp;	@Override
<i>58</i>&nbsp;	public Object save(T obj) {
<b class="fc"><i>59</i>&nbsp;		StorageMetadata&lt;T&gt; metadata = StorageMetadata.create(ctx, obj);</b>
<b class="fc"><i>60</i>&nbsp;		write(resolveFile(metadata), metadata);</b>
<b class="fc"><i>61</i>&nbsp;		return metadata.identifier;</b>
<i>62</i>&nbsp;	}
<i>63</i>&nbsp;
<i>64</i>&nbsp;	@Override
<i>65</i>&nbsp;	public void delete(T obj) {
<i>66</i>&nbsp;		try {
<b class="fc"><i>67</i>&nbsp;			FileUtils.forceDelete(resolveFile(StorageMetadata.create(ctx, obj)));</b>
<b class="nc"><i>68</i>&nbsp;		} catch (IOException e) {</b>
<b class="nc"><i>69</i>&nbsp;			throw new RuntimeException(e);</b>
<b class="fc"><i>70</i>&nbsp;		}</b>
<b class="fc"><i>71</i>&nbsp;	}</b>
<i>72</i>&nbsp;
<i>73</i>&nbsp;	@Override
<i>74</i>&nbsp;	public &lt;E extends T&gt; Collection&lt;E&gt; list(Class&lt;E&gt; type) {
<b class="fc"><i>75</i>&nbsp;		StorageMetadata&lt;E&gt; metadata = StorageMetadata.create(ctx, type);</b>
<b class="fc"><i>76</i>&nbsp;		File dir = resolveDirectory(metadata);</b>
<b class="fc"><i>77</i>&nbsp;		File[] files = dir.listFiles(fileFilter);</b>
<b class="fc"><i>78</i>&nbsp;		List&lt;E&gt; objects = new ArrayList&lt;E&gt;(files.length);</b>
<b class="fc"><i>79</i>&nbsp;		for (File file : files) {</b>
<b class="fc"><i>80</i>&nbsp;			metadata.identifier = FilenameUtils.getBaseName(file.getName());</b>
<b class="fc"><i>81</i>&nbsp;			objects.add(read(file, metadata));</b>
<i>82</i>&nbsp;		}
<b class="fc"><i>83</i>&nbsp;		return objects;</b>
<i>84</i>&nbsp;	}
<i>85</i>&nbsp;
<i>86</i>&nbsp;	@Override
<i>87</i>&nbsp;	public abstract &lt;E extends T&gt; E read(File file, StorageMetadata&lt;E&gt; metadata);
<i>88</i>&nbsp;
<i>89</i>&nbsp;	@Override
<i>90</i>&nbsp;	public abstract void write(File file, StorageMetadata&lt;T&gt; metadata);
<i>91</i>&nbsp;
<i>92</i>&nbsp;	@Override
<i>93</i>&nbsp;	public File getRootDirectory() {
<b class="fc"><i>94</i>&nbsp;		return rootDirectory;</b>
<i>95</i>&nbsp;	}
<i>96</i>&nbsp;
<i>97</i>&nbsp;	@Override
<i>98</i>&nbsp;	public String getDirectoryName(StorageMetadata&lt;? extends T&gt; metadata) {
<b class="fc"><i>99</i>&nbsp;		return metadata.entityName;</b>
<i>100</i>&nbsp;	}
<i>101</i>&nbsp;
<i>102</i>&nbsp;	@Override
<i>103</i>&nbsp;	public String getFileName(StorageMetadata&lt;? extends T&gt; metadata) {
<b class="fc"><i>104</i>&nbsp;		return metadata.identifier != null ? String.valueOf(metadata.identifier) : null;</b>
<i>105</i>&nbsp;	}
<i>106</i>&nbsp;
<i>107</i>&nbsp;	@Override
<i>108</i>&nbsp;	public String getExtension(StorageMetadata&lt;? extends T&gt; metadata) {
<b class="nc"><i>109</i>&nbsp;		return ext;</b>
<i>110</i>&nbsp;	}
<i>111</i>&nbsp;
<i>112</i>&nbsp;	protected void setExt(String ext) {
<b class="fc"><i>113</i>&nbsp;		this.ext = ext;</b>
<b class="fc"><i>114</i>&nbsp;		this.fileFilter = FileFilterUtils.and(FileFileFilter.FILE, CanReadFileFilter.CAN_READ,</b>
<i>115</i>&nbsp;				FileFilterUtils.suffixFileFilter(&#39;.&#39; + ext, IOCase.INSENSITIVE));
<b class="fc"><i>116</i>&nbsp;	}</b>
<i>117</i>&nbsp;
<i>118</i>&nbsp;	@Override
<i>119</i>&nbsp;	public File resolveDirectory(StorageMetadata&lt;? extends T&gt; metadata) {
<i>120</i>&nbsp;		try {
<b class="fc"><i>121</i>&nbsp;			File dir = new File(getRootDirectory(), getDirectoryName(metadata));</b>
<b class="fc"><i>122</i>&nbsp;			FileUtils.forceMkdir(dir);</b>
<b class="fc"><i>123</i>&nbsp;			return dir;</b>
<b class="nc"><i>124</i>&nbsp;		} catch (IOException e) {</b>
<b class="nc"><i>125</i>&nbsp;			throw new ViennaException(e);</b>
<i>126</i>&nbsp;		}
<i>127</i>&nbsp;	}
<i>128</i>&nbsp;
<i>129</i>&nbsp;	@Override
<i>130</i>&nbsp;	public File resolveFile(StorageMetadata&lt;? extends T&gt; metadata) {
<b class="fc"><i>131</i>&nbsp;		return new File(resolveDirectory(metadata), getFileName(metadata) + &#39;.&#39; + ext);</b>
<i>132</i>&nbsp;	}
<i>133</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:22</div>
</div>
</body>
</html>
